package my.vo.voImpl;

import my.vo.Vo;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author 星环
 * @date 2022/10/31 9:43:09
 * @description
 */
public class Book extends Vo {
    private String name;
    private String publishingHouse;
    private String  author;
    private int typeId;
    private int borrowerId;

    public Book(){

    }

    public Book(String name, String publishingHouse, String author, int typeId) {
        this.name = name;
        this.publishingHouse = publishingHouse;
        this.author = author;
        this.typeId = typeId;
    }

    public Book(int id, String name, String publishingHouse, String author, int typeId) {
        this.id = id;
        this.name = name;
        this.publishingHouse = publishingHouse;
        this.author = author;
        this.typeId = typeId;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPublishingHouse() {
        return publishingHouse;
    }

    public void setPublishingHouse(String publishingHouse) {
        this.publishingHouse = publishingHouse;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getTypeId() {
        return typeId;
    }

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }

    public int getBorrowerId() {
        return borrowerId;
    }

    public void setBorrowerId(int borrowerId) {
        this.borrowerId = borrowerId;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", publishingHouse='" + publishingHouse + '\'' +
                ", author='" + author + '\'' +
                ", typeId=" + typeId +
                ", borrowerId=" + borrowerId +
                '}';
    }

    public String toString(String typeName){
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", publishingHouse='" + publishingHouse + '\'' +
                ", author='" + author + '\'' +
                ", typeName=" + typeName +
                ", borrowerId=" + borrowerId +
                '}';
    }
    public String toString(String typeName,String borrowerName){
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", publishingHouse='" + publishingHouse + '\'' +
                ", author='" + author + '\'' +
                ", typeName=" + typeName +
                ", borrowerName=" + borrowerName +
                '}';
    }

    @Override
    public void saveBy(FileWriter fileWriter) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(id).append(" ").
                append(name).append(" ").
                append(publishingHouse).append(" ").
                append(author).append(" ").
                append(typeId).append(" ").
                append(borrowerId).append("\n");
        try {
            fileWriter.write(stringBuilder.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public boolean readBy(BufferedReader fileReader) {
        try {
            String s = fileReader.readLine();
            if(s == null){
                return false;
            }
            String[] fields = s.split(" ");
            if(fields.length != 6){
                return false;
            }
            int id = Integer.parseInt(fields[0]);
            setId(id);
            setName(fields[1]);
            setPublishingHouse(fields[2]);
            setAuthor(fields[3]);
            int typeId = Integer.parseInt(fields[4]);
            setTypeId(typeId);
            int borrowerId = Integer.parseInt(fields[5]);
            setBorrowerId(borrowerId);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;

    }

    @Override
    public Vo getEmptyInstance() {
        return new Book();
    }

    public void setInfo(Book book) {
        setId(book.id);
        setName(book.name);
        setPublishingHouse(book.publishingHouse);
        setAuthor(book.author);
        setTypeId(book.typeId);
        setBorrowerId(book.borrowerId);
    }
}
